<script setup>
import router from '@/router'
import {ref, reactive, onMounted} from "vue";
import {
  Menu as IconMenu,
  TrendCharts,
  UserFilled,
  Shop, ElementPlus, ShoppingCart,
} from '@element-plus/icons-vue';
import axios from "axios";
// 左侧菜单折叠状态
const isCollapse = ref(false)
// 左侧菜单数据
let menuList = reactive([]);
// 左侧二级菜单激活状态
let activePath = ref('')


function logout() {
  sessionStorage.removeItem('token');
  router.push('/login');
}

const getMenuList = async () => {
  const {data: res} = await axios.get('/menus')
  if (res.meta.status !== 200) {
    ElMessage.error(res.meta.msg);
    return;
  }
  Object.assign(menuList, res.data);
  // console.log(res.data);
  // console.log(menuList);
}
onMounted(() => {
  getMenuList();
  activePath.value = sessionStorage.getItem('activePath');
  // console.log("mount");
  // console.log(typeof menuList.value);

});
// watchEffect(() => {
//   getMenuList();
//   console.log("watch");
// });
// 切换菜单的折叠与展开
function toggleCollapse() {
  isCollapse.value = !isCollapse.value;
}

// 保存菜单激活状态
function saveNavState(path) {
  sessionStorage.setItem('activePath', path);
  activePath.value = path;
}
</script>

<template>
  <el-container class="home-container">
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="@/assets/heima.png" alt="pic">
        <span>Vue3 + Element-Plus</span>
      </div>
      <el-button @click="logout()" type="info">退出登录</el-button>
    </el-header>
    <!-- 主体内容 -->
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px' ">
        <!-- 侧边栏菜单区域 -->
        <div class="toggle-button" @click="toggleCollapse()">|||</div>
        <el-menu
            active-text-color="#409EFF"
            background-color="#333744"
            text-color="#fff"
            :unique-opened="true"
            :collapse="isCollapse"
            :collapse-transition="false"
            :router="true"
            :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <el-sub-menu :index="item.id + ''"
                       v-for="item in menuList"
                       :key="item.id">
            <template #title>
              <!-- 图标 -->
              <el-icon v-if="item.id===125">
                <UserFilled/>
              </el-icon>
              <el-icon v-if="item.id===103">
                <ElementPlus/>
              </el-icon>
              <el-icon v-if="item.id===101">
                <Shop/>
              </el-icon>
              <el-icon v-if="item.id===102">
                <ShoppingCart/>
              </el-icon>
              <el-icon v-if="item.id===145">
                <TrendCharts/>
              </el-icon>

              <!-- 菜单文本 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <!--  index 指向路由地址-->
            <el-menu-item :index="'/'+subItem.path"
                          v-for="subItem in item.children"
                          :key="subItem.id"
                          @click="saveNavState('/'+subItem.path)">
              <template #title>
                <!-- 图标 -->
                <el-icon>
                  <IconMenu/>
                </el-icon>
                <!-- 菜单文本 -->
                <span> {{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-sub-menu>

        </el-menu>
      </el-aside>
      <!-- 右侧内容 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>

<style lang="less" scoped>

.el-header {
  background-color: #373D41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;

  div {
    display: flex;
    align-items: center;

    span {
      margin-left: 15px;
    }
  }

}

.el-aside {
  background-color: #333744;

  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}

.home-container {
  height: 100%;
}

.toggle-button {
  background-color: #4A5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2px;
  cursor: pointer;
}

</style>
